
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO
Imports _2bMatesSite.Controls

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_group_events.
	''' </summary>
	Public Partial Class view_group_events
		Inherits BaseWebPage
		Protected tblTopTable As System.Web.UI.WebControls.Table

		Protected grpEvents As ArrayList = Nothing
		Protected grpID As Integer = -1
		Protected id As Integer = -1
		Protected usr As User = Nothing
		Protected grp As Group = Nothing
		Protected grpEvent As GroupEvent = Nothing
		Protected nGroupEventsCount As Double = 0
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

			' Put user code to initialize the page here

			usr = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			If Request.Params(Constants.GROUP_PARAM) = Nothing OrElse Request.Params(Constants.GROUP_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Me.initPageTitles(Constants.VIEW_GROUP_EVENTS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			Try
				grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
				grp = Groups.GetGroupByID(grpID)
				grpEvents = GroupEvents.LoadGroupEvents(grpID)
				nGroupEventsCount = grpEvents.Count

				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
				End If
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			'litTitle.Text = "Events & Shows";
			litRightLinks.Text = "<a href='" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID + "' class=""white_link"">" + (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name) + "</a>"
			If nGroupEventsCount = 0 Then
				tblGroupEvents.Visible = False
				lblNoGroupEvents.Visible = True
			Else
				' init Pagers
				dgPager.RedirectPage = Constants.VIEW_GROUP_EVENTS + "?" + Constants.GROUP_PARAM + "=" + grp.ID
				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nGroupEventsCount / Constants.MAX_GRID_EVENTS_ON_VIEW_PAGE))
				dgPagerUp.RedirectPage = Constants.VIEW_GROUP_EVENTS + "?" + Constants.GROUP_PARAM + "=" + grp.ID
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nGroupEventsCount / Constants.MAX_GRID_EVENTS_ON_VIEW_PAGE))
				dgPager.Action = "-1"
				dgPagerUp.Action = "-1"

				' initialize listing literals

				dgPager.dgListing.TotalCount = Convert.ToInt32(nGroupEventsCount)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(nGroupEventsCount)

				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					ViewState("StartIndex") = 0
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_GRID_EVENTS_ON_VIEW_PAGE
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_GRID_EVENTS_ON_VIEW_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_GRID_EVENTS_ON_VIEW_PAGE)

				If Not Me.IsPostBack Then
					dgGroupEvents.VirtualItemCount = Convert.ToInt32(nGroupEventsCount)
					BindGrid()
				End If
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Function CreateDataSource() As ICollection
			Dim dt As New DataTable()

			If grpEvents.Count <= 0 Then
				dgPager.FirstPageNumber = 1
				dgPager.LastPageNumber = 1
				dgPager.MaxPageNumber = 1
				dgPager.PagesRest = 1
				dgPager.dgListing.StartIndex = 0
				dgPager.dgListing.EndIndex = 0
				dgPagerUp.FirstPageNumber = 1
				dgPagerUp.LastPageNumber = 1
				dgPagerUp.MaxPageNumber = 1
				dgPagerUp.PagesRest = 1
				dgPagerUp.dgListing.StartIndex = 0
				dgPagerUp.dgListing.EndIndex = 0
				dgGroupEvents.AllowSorting = False
			Else
				Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
				Dim endIndex As Integer = startIndex + Constants.MAX_GRID_EVENTS_ON_VIEW_PAGE
				Dim t As Integer = 0
				Dim counter As Integer = 0
				endIndex = Math.Min(endIndex, Convert.ToInt32(nGroupEventsCount))
				t = (endIndex - startIndex)
				dgPager.dgListing.StartIndex = startIndex + 1
				dgPager.dgListing.EndIndex = endIndex
				dgPagerUp.dgListing.StartIndex = startIndex + 1
				dgPagerUp.dgListing.EndIndex = endIndex

				Dim dr As DataRow

				dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("EventDate", GetType(String)))
				dt.Columns.Add(New DataColumn("EventTime", GetType(String)))
				dt.Columns.Add(New DataColumn("Venue", GetType(String)))
				dt.Columns.Add(New DataColumn("Address", GetType(String)))
				dt.Columns.Add(New DataColumn("City", GetType(String)))
				dt.Columns.Add(New DataColumn("ZipCode", GetType(String)))
				dt.Columns.Add(New DataColumn("Cost", GetType(String)))
				dt.Columns.Add(New DataColumn("Details", GetType(String)))

				' List group events
				For Each result As GroupEvent In grpEvents
					If counter >= startIndex AndAlso counter < endIndex Then
						dr = dt.NewRow()
						dr(0) = result.ID
						If result.DateTimeEvent.ToString() <> Nothing Then
							' Add date / time
							dr(1) = result.DateTimeEvent.ToString("dddd, MMMM dd yyyy")
						Else
							dr(1) = ""
						End If
						If result.Venue <> Nothing Then
							dr(2) = result.DateTimeEvent.ToString("h:mm tt")
						Else
							dr(2) = ""
						End If
						If result.Venue <> Nothing Then
							dr(3) = result.Venue
						Else
							dr(3) = ""
						End If
						If result.Address <> Nothing Then
							dr(4) = result.Address
						Else
							dr(4) = ""
						End If
						If result.City <> Nothing Then
							dr(5) = result.City
						Else
							dr(5) = ""
						End If
						If result.ZipCode <> Nothing Then
							dr(6) = result.ZipCode
						Else
							dr(6) = ""
						End If
						If result.Cost <> Nothing Then
							dr(7) = result.Cost
						Else
							dr(7) = ""
						End If
						If result.Details <> Nothing Then
							dr(8) = result.Details
						Else
							dr(8) = ""
						End If
						dt.Rows.Add(dr)
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
				Next
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			' init datagrid with database data 
			dgGroupEvents.DataSource = CreateDataSource()
			dgGroupEvents.DataBind()
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






